package com.aiyige.utils;

import android.text.InputFilter;
import android.text.Spanned;
import android.util.Log;

import com.aiyige.R;

/**
 * Created by zhuyakun on 2017/11/10.
 */

public class InputNumFilter implements InputFilter {
    public static final String TAG = InputNumFilter.class.getSimpleName();
    int maxInputNum;
    int maxCount;
    Listener listener;
    boolean hasReachMaxInputNum = false;
    boolean mute = true;

    public InputNumFilter(int maxInputNum) {
        this(maxInputNum, true);
    }

    public InputNumFilter(int maxInputNum, boolean mute) {
        this.maxInputNum = maxInputNum;
        maxCount = maxInputNum * 2;
        hasReachMaxInputNum = false;
        this.mute = mute;
    }


    public Listener getListener() {
        return listener;
    }

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        int count = 0;
        for (int i = 0; i < dstart; ++i) {
            if (dest.charAt(i) < 128) {
                count += 1;
            } else {
                count += 2;
            }
            if (count > maxCount) {
                if (!hasReachMaxInputNum) {
                    hasReachMaxInputNum = true;
                    if (!mute) {
                        ToastX.show(String.format(StringUtils.getString(R.string.reach_max_input_limit), maxInputNum));
                    }
                }
                if (listener != null) {
                    listener.inputNum(maxInputNum);
                }
                return "";
            }
        }

        for (int i = dend + 1; i < dest.length(); ++i) {
            if (dest.charAt(i) < 128) {
                count += 1;
            } else {
                count += 2;
            }
            if (count > maxCount) {
                if (!hasReachMaxInputNum) {
                    hasReachMaxInputNum = true;
                    if (!mute) {
                        ToastX.show(String.format(StringUtils.getString(R.string.reach_max_input_limit), maxInputNum));
                    }
                }
                if (listener != null) {
                    listener.inputNum(maxInputNum);
                }
                return "";
            }
        }

        for (int i = 0; i < source.length(); ++i) {
            if (source.charAt(i) < 128) {
                count += 1;
            } else {
                count += 2;
            }
            if (count > maxCount) {
                if (!hasReachMaxInputNum) {
                    hasReachMaxInputNum = true;
                    if (!mute) {
                        ToastX.show(String.format(StringUtils.getString(R.string.reach_max_input_limit), maxInputNum));
                    }
                }
                if (listener != null) {
                    listener.inputNum(maxInputNum);
                }
                return source.subSequence(0, i);
            }
        }
        if (listener != null) {
            listener.inputNum(count / 2 + count % 2);
        }
        hasReachMaxInputNum = false;
        return source;
    }


    public interface Listener {
        void inputNum(int num);
    }

}
